home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Disk & File / Disk / CDRom / DontShareIt Lite / DontShareIt Lite.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  8KB  |  273 lines

  1. /* Don’tShareIt... Lite */
  2. /* by Jim Luther */
  3.  
  4. #include "DontShareIt Lite.h"
  5.  
  6. /* A4 globals */
  7.  
  8. Ptr            gGetVolInfoLink;
  9. Boolean        gProcessMgrAvailable;
  10. long        gAttribs;
  11. short        gAFPTranslatorRefNum;
  12. Ptr            gSetTrapAddrLink;
  13. Boolean        gHasRun;
  14.  
  15.  
  16. Ptr PatchTrap(short trapWord, short trapType, ProcPtr newAddress)
  17. /* Patches specified A-Trap and returns the old A-Trap routine address */
  18. {
  19.     long oldAddress;
  20.     
  21.     newAddress = StripAddress((Ptr)newAddress);
  22.     oldAddress = NGetTrapAddress(trapWord,trapType);
  23.     NSetTrapAddress((long)newAddress,trapWord,trapType);
  24.     
  25.     return (Ptr)oldAddress;
  26. }
  27.  
  28.  
  29. Boolean IsTargetApp(void)
  30. /* See if the current process is File Sharing, AppleShare, or AppleShare Admin. */
  31. /* Call GetProcessInformation and check the creator type returned against */
  32. /* 'hhgg' (File Sharing), 'hhge' (AppleShare), and 'admn' (AppleShare Admin). */
  33. {
  34.     OSErr result;
  35.     ProcessSerialNumber currentPSN;
  36.     ProcessInfoRec info;
  37.     short theCount;
  38.     
  39.     /* If Process Manager wasn't available last time we looked, see if it is now. */
  40.     if (!gProcessMgrAvailable) {
  41.         gProcessMgrAvailable = ((Gestalt(gestaltOSAttr, &gAttribs) == noErr) && 
  42.                                 (gAttribs & (1 << gestaltLaunchControl)));
  43.         /* If Process Manager still isn't available, then the target processes aren't. */
  44.         if (!gProcessMgrAvailable)
  45.             return (false);
  46.     }
  47.         
  48.     /* Do a quick check of the current application for the Finder since it polls
  49.     ** GetVolInfo very heavily. This saves a call to GetProcessInformation which
  50.     ** probably reduces any performance hit this patch makes on the system.
  51.     ** If someone were to rename one of the apps I'm checking for to the
  52.     ** FinderName, this wouldn't work. I hope that doesn't happen... */
  53.     if (CurApName[0] == FinderName[0]) {
  54.         theCount = 1;
  55.         while (theCount <= CurApName[0]) {
  56.             if (CurApName[theCount] != FinderName[theCount])
  57.                 break;
  58.             theCount++;
  59.         }
  60.         if (theCount == CurApName[0]+1)
  61.             return (false); /* CurApName == FinderName */
  62.     }
  63.     
  64.     /* Get the current process' creator type with GetProcessInformation */
  65.     currentPSN.highLongOfPSN = 0;
  66.     currentPSN.lowLongOfPSN = kCurrentProcess;
  67.     info.processInfoLength = sizeof(ProcessInfoRec);
  68.     info.processName = nil;        /* we don't want the process name */
  69.     info.processAppSpec = nil;    /* we don't want the process file location */
  70.     result = GetProcessInformation(¤tPSN, &info);
  71.     if (result == noErr)
  72.         /* See if the current process is one of the target processes. */
  73.         switch (info.processSignature) {
  74.             case kFileSharingCreator:
  75.             case kAppleShareCreator:
  76.             case kASAdminCreator:
  77.                 return (true);
  78.                 break;
  79.         }
  80.     return (false);
  81. }
  82.  
  83.  
  84. OSErr PGetVolInfo(void)
  85. /* Here's the patch to _GetVolInfo */
  86. {
  87.     HParmBlkPtr    pBlock;        /* pointer to the parameter block */
  88.     short        trapWord;    /* the trapword */
  89.     Ptr            nextLink;    /* the GetVolInfo routine before we patched */
  90.     Boolean        myVolNameBuffer; /* did we supply the volume name buffer? */
  91.     OSErr        result;        /* result to return to caller */
  92.     
  93.     /* Get register values before they're blown away */
  94.     asm {
  95.         move.w    d1,trapWord        ; get the trapword from D1
  96.         move.l    a0,pBlock        ; get the parameter block pointer from A0
  97.     }
  98.     
  99.     SetUpA4(); /* set up the A4 world */
  100.     
  101.     nextLink = gGetVolInfoLink; /* get the next link from gGetVolInfoLink */
  102.  
  103.     if (((trapWord & kAsyncBit) != 0) ||        /* if the call is async or */
  104.         ((trapWord & kHFSBit) == 0))            /* the call isn't _HGetVInfo */
  105.         /* jump to the next link (head patch only) */
  106.         asm {
  107.             move.w    trapWord,d1        ; restore the trapword to D1
  108.             move.l    pBlock,a0        ; restore the parameter block pointer to A0
  109.             move.l    nextLink,a1        ; get nextLink
  110.             move.l    (sp)+,a4        ; do what RestoreA4 normally does
  111.             unlk    a6                ; unlink A6
  112.             move.l    a1,-(sp)        ; put nextLink on the stack and
  113.             rts                        ; return to it
  114.         }
  115.     
  116.     /* This is a synchronous _HGetVInfo call */ 
  117.     
  118.     /* go ahead and make the call and we'll do more in the tail patch. */
  119.     asm { 
  120.         move.w    trapWord,d1        ; restore the trapword to D1
  121.         move.l    pBlock,a0        ; restore the parameter block pointer to A0
  122.         move.l    nextLink,a1        ; get nextLink
  123.         jsr        (a1)            ; and call it...
  124.     }
  125.     
  126.     /* If errors, then do nothing */
  127.     if (pBlock->volumeParam.ioResult == noErr) {
  128.  
  129.         /* We know that we can skip any volumed owned by the .Sony driver ($fffb)
  130.            or drives owned by the .AFPTranslator driver because the file server
  131.            already ignores them. */
  132.         if ((pBlock->volumeParam.ioVDRefNum != SonyRefNum) &&
  133.             (pBlock->volumeParam.ioVDRefNum != gAFPTranslatorRefNum)) {
  134.             
  135.             /* Check for the target applications */
  136.             if (IsTargetApp()) {
  137.                 /* Is this volume owned by Audio CD Access? */
  138.                 if (pBlock->volumeParam.ioVFSID == 0x4A48) {
  139.                     /* Yes, now we'll make this volume unusable by the file server. */
  140.                     /* The only ways to do that are to make ioVDRefNum == 0xfffb (.Sony), */
  141.                     /* make ioVFSID == the file system ID used by the AppleShare workstation, */
  142.                     /* or to make ioVSigWord != 0x4244 (HFS volume). */
  143.                     /* I'll simply clear ioVSigWord which should do the trick nicely. */
  144.                     pBlock->volumeParam.ioVSigWord = 0;
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     
  150.     RestoreA4();
  151.     return (pBlock->volumeParam.ioResult);
  152. }
  153.  
  154.  
  155. short NumToolboxTraps(void)
  156. {
  157.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  158.         return (0x200);
  159.     else
  160.         return (0x400);
  161. }
  162.  
  163.  
  164. TrapType GetTrapType(short theTrap)
  165. {
  166.     if ((theTrap & 0x0800) > 0)
  167.         return (ToolTrap);
  168.     else
  169.         return (OSTrap);
  170. }
  171.  
  172.  
  173. Boolean    TrapAvailable(short theTrap)
  174. {
  175.     TrapType tType;
  176.     
  177.     tType = GetTrapType(theTrap);
  178.     if (tType == ToolTrap) {
  179.         theTrap = theTrap & 0x07ff;
  180.         if (theTrap >= NumToolboxTraps())
  181.             theTrap = _Unimplemented;
  182.     }
  183.     return (NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  184. }
  185.  
  186.  
  187. void PatchGetVolInfo(void)
  188. {
  189.     /* Find out if the process manager is available (it shouldn't be yet) */
  190.     gProcessMgrAvailable = ((Gestalt(gestaltOSAttr, &gAttribs) == noErr) && 
  191.                             (gAttribs & (1 << gestaltLaunchControl)));
  192.     
  193.     /* Get the .AFPTranslator driver's reference number */
  194.     if (OpenDriver(kAFPTranslatorName, &gAFPTranslatorRefNum) != noErr)
  195.         gAFPTranslatorRefNum = 0;
  196.     
  197.     /* Patch _GetVolInfo (A-Trap $A007) */
  198.     gGetVolInfoLink = PatchTrap(_GetVolInfo, OSTrap, PGetVolInfo);
  199. }
  200.  
  201.  
  202. void SetTrapAddrPatch(void)
  203. {
  204.     Ptr nextLink;
  205.     short trapWord;
  206.     short callerTrapWord;    // the trap word used to make this call is in d1
  207.     
  208.     asm {
  209.         movem.l    a0-a5/d0-d7,-(sp)
  210.         move.w    d0,trapWord
  211.         move.w    d1,callerTrapWord
  212.     }
  213.     
  214.     SetUpA4();
  215.     nextLink = gSetTrapAddrLink;
  216.     
  217.     if (!gHasRun && 
  218.         (trapWord == _ServerDispatch) && 
  219.         ((callerTrapWord & kTrapTypeMask) == kOSTrapType)) {
  220.  
  221.         // _ServerDispatch trap is being installed
  222.         gHasRun = true;
  223.         
  224.         PatchGetVolInfo();
  225.     }
  226.     
  227.     asm{
  228.         move.l    (sp)+,a4            ; same as RestoreA4()
  229.         movem.l    (sp)+,a0-a5/d0-d7
  230.         move.l    nextLink,a1
  231.         unlk    a6
  232.         jmp        (a1)
  233.     }
  234. }
  235.  
  236.  
  237. void main(void)
  238. {
  239.     Ptr codePtr;
  240.     Handle codeHndl;
  241.     
  242.     asm {
  243.         move.l    a0,codePtr    ; get point to our code
  244.     }
  245.     
  246.     /* Register A0 contains pointer to my code resource */
  247.     RememberA0();    /* save A0 where we can get to it later */
  248.     SetUpA4();        /* and set up the A4 world */
  249.  
  250.     codeHndl = RecoverHandle(codePtr); /* get handle to our code */
  251.     DetachResource(codeHndl);    /* and detach our code resource */
  252.     
  253.     if (TrapAvailable(_ServerDispatch)) {
  254.     
  255.         /* install _GetVolInfo patch now */
  256.         
  257.         PatchGetVolInfo();
  258.     }
  259.     else {
  260.     
  261.         /* Install patch to SetTrapAddress when _ServerDispatch is installed. */
  262.         /* That's where the _GetVolInfo patch will be installed if the AppleShare */
  263.         /* of File Sharing server software is installed. */
  264.         
  265.         gHasRun = false;    /* The SetTrapAddress patch for _ServerDispatch */
  266.                             /* hasn't been executed. */
  267.     
  268.         gSetTrapAddrLink = PatchTrap(_SetTrapAddress, OSTrap, SetTrapAddrPatch);
  269.     }
  270.     
  271.     RestoreA4();
  272. }
  273.